package interfacePackage;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.WindowConstants;

public class FramePopupRechercherVille extends JFrame implements ActionListener {

	private static final long serialVersionUID = 1L;

	private JPanel jContentPane = null;

	private static FramePopupRechercherVille m_instance;
	
	/**
	 * This is the default constructor
	 */
	private FramePopupRechercherVille() {
		super();
		initialize();
	}
	
	public static FramePopupRechercherVille getInstance() {
		if (null == m_instance) { // Premier appel
			m_instance = new FramePopupRechercherVille();
        }
        return m_instance;
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
	//	int width = PanelPopupRechercherVille.getInstance().getWidth() + this.getInsets().left + this.getInsets().right;
	//	int height = 30 + PanelPopupRechercherVille.getInstance().getHeight() + this.getInsets().top + this.getInsets().bottom;
		int width = 300;
		int height = 200;
		
		this.setSize(width, height);
		this.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
		this.setContentPane(getJContentPane());
		this.setTitle("Rechercher une ville");
		this.setAlwaysOnTop(true);
		this.repaint();
		this.addWindowListener( new WindowAdapter() {
             // This window listener responds when the user
             // clicks the window's close box by giving the
             // user a chance to change his mind.
          public void windowClosing(WindowEvent evt) {
        	  PanelItineraireChoix.getInstance().actionPerformed(new ActionEvent(evt.getWindow(), ActionEvent.ACTION_PERFORMED, PanelItineraireChoix.FRAME_POPUP_CHERCHER_VILLE_FERMEE));
        	  FramePopupRechercherVille.getInstance().dispose();
        	  reInit();
          }
       });

	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(new BorderLayout());
			jContentPane.add(PanelPopupRechercherVille.getInstance());
		}
		return jContentPane;
	}

	public void actionPerformed(ActionEvent arg0) {
		System.out.println("action sur frame popup " + arg0);
		if (PanelPopupRechercherVille.POPUP_VILLES_VALIDER.equals(arg0.getActionCommand())) {
			PanelPopupRechercherVilleSolutions.getInstance().tuerInstance() ;
			jContentPane.removeAll();
			PanelPopupRechercherVilleSolutions.getInstance().initialize();
			
			//PanelPopupRechercherVilleSolutions.reInit();
			jContentPane.add(PanelPopupRechercherVilleSolutions.getInstance());
			jContentPane.repaint();

			FramePopupRechercherVille.getInstance().setSize(300,250); //rafraichit la liste ! a laisser
			System.out.println("event PanelPopupRechercherVille nom de ville " + PanelPopupRechercherVille.getNomVille());
	    }
		if (PanelPopupRechercherVilleSolutions.POPUP_VILLES_SOLUTIONS_VALIDER.equals(arg0.getActionCommand())) {
			reInit();
			this.dispose();
			
			//ville intermediaire ou dans le panel Itineraire choix ? on regarde le bouton clique au depart :
			if (PanelItineraireChoix.getInstance().getButtonSearchClicked().equals(PanelItineraireChoix.getInstance().SEARCH_VILLE_INTERMEDIAIRE))
				PanelPopupAjouterVilleIntermediaire.getInstance().actionPerformed(arg0);
			else {
				PanelItineraireChoix.getInstance().actionPerformed(arg0);
			}
			TobtobFenetre.getInstance().setVisible(true);
			System.out.println("event PanelPopupRechercherVilleSolutions nom de ville " + PanelPopupRechercherVilleSolutions.getNomVille());
			
		}
	}
	private void reInit() {
		jContentPane.removeAll();
		jContentPane.add(PanelPopupRechercherVille.getInstance());
		PanelPopupRechercherVille.setNomVille("");
		jContentPane.repaint();
	}
}
